//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.xw.service;

import com.github.pagehelper.PageHelper;
import com.xw.common.Const;
import com.xw.dao.*;
import com.xw.model.SysUserConcern;
import com.xw.model.User;
import com.xw.model.UserInfo;
import com.xw.model.Videos;
import com.xw.util.AppUtil;
import com.xw.util.JsonData;
import com.xw.vo.UserConcernVo;
import com.xw.vo.UserInfoVo;
import com.xw.vo.VideosVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private SysUserInfoMapper userInfoMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private VideosMapper videosMapper;
    @Resource
    private UserConcernMapper userConcernMapper;
    @Resource
    private VideosItemMapper videosItemMapper;

    @Override
    public Map findTeacherDetailMP(String userId, String teacherUserId) {
        TeacherVo teacherVo = userInfoMapper.findTeacherDetailByUserIdMP(userId,teacherUserId);
        return JsonData.success(teacherVo);
    }

    @Override
    public Map findRecommendTeacherMP(String userId, Integer page, Integer limit) {
        if (page != null && limit != null){
            PageHelper.startPage(page,limit,false);
        }
        List<TeacherVo> list = userInfoMapper.findRecommendTeacherMP(userId);
        return JsonData.success(list);
    }

    /**
     * @需求描述:查看讲师列表业务逻辑层
     * @创建人:whh
     * @创建时间: 2019/11/12
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @Override
    public Map<String, Object> getTeacherList(Map<String, Object> map) {

        //查找列表
        Integer pageNum = null;
        Integer pageSize = null;
        if (map.get("pageNum") != null){
            pageNum = Integer.parseInt(map.get("pageNum").toString());
        }
        if (map.get("pageSize") != null){
            pageSize = Integer.parseInt(map.get("pageSize").toString());
        }
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize, false);
        }
        List<UserInfoVo> teacherList = userInfoMapper.getTeacherList();
        map.clear();
        //讲师数量
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setIsTeacher(2);
        Integer teacherCount = userInfoMapper.selectCount(userInfoVo);
        map.put("teacherList", teacherList);
        map.put("teacherCount", teacherCount);

        return JsonData.success(map, Const.OPREATE_MSG_SUCCESS);
    }

    @Override
    public Map<String, Object> showTeacherList() {
        List<UserInfoVo> teacherList = userInfoMapper.getTeacherList();
        return  JsonData.success(teacherList, Const.OPREATE_MSG_SUCCESS);
    }

    /**
     * @需求描述:用户信息
     * @创建人:whh
     * @创建时间: 2019/11/14
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @Override
    public Map<String, Object> findUserInfo(Map<String, Object> map) {

        UserInfoVo userInfoVo = (UserInfoVo) map.get("userInfoVo");
        if (null == userInfoVo.getUserId() || "" == userInfoVo.getUserId()) {
            return JsonData.fail(Const.PARA_IS_NULL);
        }

        userInfoVo = userInfoMapper.findUserInfo(userInfoVo);
        map.clear();
        map.put("userInfoVo", userInfoVo);
        return JsonData.success(map, Const.OPREATE_MSG_SUCCESS);
    }


    /**
     * @需求描述:关注/取消关注
     * @创建人:whh
     * @创建时间: 2019/11/14
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @Override
    public Map<String, Object> unfollowTeacher(Map<String, Object> paramsMap) {

        SysUserConcern userConcernVo = (SysUserConcern) paramsMap.get("userConcernVo");

        if (userConcernVo.getStatus() == 2) {
            //取消关注
            SysUserConcern userConcern1 = new SysUserConcern();
            userConcern1.setStatus(1);
            userConcern1.setUserId(userConcernVo.getUserId());
            userConcern1.setTeacherId(userConcernVo.getTeacherId());
            List<SysUserConcern> userConcernVos = userConcernMapper.select(userConcern1);
            if (userConcernVos.size() == 0) {
                return JsonData.fail("关注信息不存在");
            }
            userConcern1 = userConcernVos.get(0);
            userConcern1.setStatus(2);
            userConcern1.setUpdateTime(new Date());
            Integer result = userConcernMapper.updateByPrimaryKey(userConcern1);
            return JsonData.success(result, Const.OPREATE_MSG_SUCCESS);
        }
        if (userConcernVo.getStatus() == 1) {
            //重新关注讲师
            userConcernVo.setStatus(null);
            List<SysUserConcern> userConcernVos = userConcernMapper.select(userConcernVo);
            if (userConcernVos.size() != 0 && userConcernVos.get(0).getStatus() == 1) {
                return JsonData.fail("您已关注此讲师，无需重复关注");
            }
            if (userConcernVos.size() != 0 && userConcernVos.get(0).getStatus() == 2) {
                userConcernVo = userConcernVos.get(0);
                userConcernVo.setStatus(1);
                userConcernVo.setUpdateTime(new Date());
                Integer result = userConcernMapper.updateByPrimaryKey(userConcernVo);
                if (1 == result) {
                    return JsonData.success(result, Const.OPREATE_MSG_SUCCESS);
                }
            } else {
                //第一次关注讲师
                userConcernVo.setStatus(1);
                userConcernVo.setConcernId(AppUtil.uuid());
                userConcernVo.setCreateTime(new Date());
                userConcernVo.setUpdateTime(new Date());
                Integer result = userConcernMapper.insertSelective(userConcernVo);
                if (1 == result) {
                    return JsonData.success(result, Const.OPREATE_MSG_SUCCESS);
                }
            }
        }
        return JsonData.fail(Const.OPREATE_MSG_ERROR);
    }

    /**
     * @需求描述:我的粉丝业务逻辑层
     * @创建人:whh
     * @创建时间: 2019/11/14
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @Override
    public Map<String, Object> findMyFans(Map<String, Object> paramsMap) {
        UserInfoVo userInfoVo = (UserInfoVo) paramsMap.get("userInfoVo");
        if (null == userInfoVo.getUserId() || "" == userInfoVo.getUserId()) {
            return JsonData.fail(Const.PARA_IS_NULL);
        }

        Integer pageNum = null;
        Integer pageSize = null;
        if (paramsMap.get("pageNum") != null){
            pageNum = Integer.parseInt(paramsMap.get("pageNum").toString());
        }
        if (paramsMap.get("pageSize") != null){
            pageSize = Integer.parseInt(paramsMap.get("pageSize").toString());
        }
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize, false);
        }
        List<UserInfoVo> userInfoVos = userInfoMapper.findMyFans(userInfoVo);
        UserConcernVo userConcernVo = new UserConcernVo();
        for (UserInfoVo infoVo : userInfoVos) {
            if(infoVo.getIsTeacher() == 2){
                userConcernVo.setTeacherId(infoVo.getUserId());
                userConcernVo.setUserId(userInfoVo.getUserId());
                userConcernVo.setStatus(1);
                List<SysUserConcern> select = this.userConcernMapper.select(userConcernVo);
                if(select.size() > 0){
                    infoVo.setIsCare(1);
                }else{
                    infoVo.setIsCare(0);
                }
            }else{
                infoVo.setIsCare(0);
            }
        }
        return JsonData.success(userInfoVos, Const.OPREATE_MSG_SUCCESS);
    }

    /**
     * @需求描述:我的关注业务逻辑层
     * @创建人:whh
     * @创建时间: 2019/11/14
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @Override
    public Map<String, Object> getMyCare(Map<String, Object> map) {
        UserInfoVo userInfoVo = (UserInfoVo) map.get("userInfoVo");
        if (null == userInfoVo.getUserId() || "" == userInfoVo.getUserId()) {
            return JsonData.fail(Const.PARA_IS_NULL);
        }

        Integer pageNum = null;
        Integer pageSize = null;
        if (map.get("pageNum") != null){
            pageNum = Integer.parseInt(map.get("pageNum").toString());
        }
        if (map.get("pageSize") != null){
            pageSize = Integer.parseInt(map.get("pageSize").toString());
        }
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize, false);
        }
        List<UserInfoVo> userInfoVos = userInfoMapper.getMyCare(userInfoVo);
        return JsonData.success(userInfoVos, Const.OPREATE_MSG_SUCCESS);
    }


    /**
     * @需求描述:添加/修改用户资料
     * @创建人:whh
     * @创建时间: 2019/11/15
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @Override
    public Map<String, Object> changeUserInfo(Map<String, Object> map) {

        UserInfoVo userInfoVo =(UserInfoVo)map.get("userInfoVo");

        if(userInfoVo.getUiId() !=null){
            //修改信息
            UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userInfoVo.getUiId());
            userInfo.setGender(userInfoVo.getGender());
            userInfo.setBirth(userInfoVo.getBirth());
            userInfo.setUserArea(userInfoVo.getUserArea());
            userInfo.setUserDesc(userInfoVo.getUserDesc());
            userInfo.setUserJob(userInfoVo.getUserJob());
            userInfo.setUserSdf(userInfoVo.getUserSdf());
            int index = userInfoMapper.updateByPrimaryKey(userInfo);

            //修改昵称
            User user =userMapper.selectByPrimaryKey(userInfoVo.getUserId());
            user.setUserNickName(userInfoVo.getUserNickName());
            user.setUserImg(userInfoVo.getUserImg());
            user.setUpdateTime(new Date());
            int result = userMapper.updateByPrimaryKey(user);
            if (result == 1 && index == 1){
                videosItemMapper.updateTeacherNameByUserId(userInfoVo);
                return JsonData.success(Const.OPREATE_MSG_SUCCESS);
            }
        }else {
            //添加信息
            userInfoVo.setUiId(AppUtil.uuid());
            int insert = userInfoMapper.insertSelective(userInfoVo);
            //修改昵称
            User user =userMapper.selectByPrimaryKey(userInfoVo.getUserId());
            user.setUserNickName(userInfoVo.getUserNickName());
            user.setUserImg(userInfoVo.getUserImg());
            user.setUpdateTime(new Date());
            int result = userMapper.updateByPrimaryKey(user);
            if (result == 1 && insert == 1){
                videosItemMapper.updateTeacherNameByUserId(userInfoVo);
                return JsonData.success(Const.OPREATE_MSG_SUCCESS);
            }

        }

    return JsonData.fail(Const.OPREATE_MSG_ERROR);

    }

    /**
     * @需求描述:讲师详情接口
     * @创建人:whh
     * @创建时间: 2019/11/12
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @Override
    public Map<String, Object> getTeacherInfo(Map<String, Object> map) {
        String fansInfoId = (String) map.get("fansInfoId");
        UserInfoVo userInfoVo = (UserInfoVo) map.get("userInfoVo");
        UserConcernVo userConcernVo = new UserConcernVo();
        boolean isConcern = false;
        if(null != fansInfoId){
            userConcernVo.setUserId(fansInfoId);
            userConcernVo.setTeacherId(userInfoVo.getUserId());
            userConcernVo.setStatus(1);
            Integer integer =  userConcernMapper.selectCount(userConcernVo);
            if (integer >0 ){
                isConcern = true;
            }
        }
        if (null == userInfoVo.getUserId() || "" == userInfoVo.getUserId()) {
            return JsonData.fail(Const.PARA_IS_NULL);
        }
        List<UserInfoVo> userInfoVos = userInfoMapper.showTeachers(userInfoVo);
        VideosVo videosVo = new VideosVo();
        videosVo.setCheckStatus(1);
        videosVo.setIsPublish(1);
        videosVo.setIsDel(0);
        for (UserInfoVo userInfoVo1 : userInfoVos) {
            //查询课程结果集
            videosVo.setIsPublish(1);
            videosVo.setUserId(userInfoVo1.getUserId());
            PageHelper.orderBy("is_top desc");
            List<Videos> videos = videosMapper.select(videosVo);
            userInfoVo1.setVideosVos(videos);
            userInfoVo1.setVideoCount(videos.size());
        }
        map.clear();
        map.put("userInfoVos",userInfoVos);
        map.put("isConcern",isConcern);
        return JsonData.success(map, Const.OPREATE_MSG_SUCCESS);
    }


    @Override
    public Map<String, Object> findTeacherInfo(Map<String, Object> map) {
        String fansInfoId = (String) map.get("fansInfoId");
        UserInfoVo userInfoVo = (UserInfoVo) map.get("userInfoVo");
        UserConcernVo userConcernVo = new UserConcernVo();
        userConcernVo.setUserId(fansInfoId);
        userConcernVo.setTeacherId(userInfoVo.getUserId());
        userConcernVo.setStatus(1);
        Integer integer =  userConcernMapper.selectCount(userConcernVo);
        boolean isConcern = false;
        if (integer >0 ){
            isConcern = true;
        }

        if (null == userInfoVo.getUserId() || "" == userInfoVo.getUserId()) {
            return JsonData.fail(Const.PARA_IS_NULL);
        }

        List<UserInfoVo> userInfoVos = userInfoMapper.showTeachers(userInfoVo);
        VideosVo videosVo = new VideosVo();
        videosVo.setCheckStatus(1);
        videosVo.setIsDel(0);
        for (UserInfoVo userInfoVo1 : userInfoVos) {
            //查询课程结果集
            videosVo.setCheckStatus(1);
            videosVo.setUserId(userInfoVo1.getUserId());
            List<Videos> videos = videosMapper.select(videosVo);
            userInfoVo1.setVideosVos(videos);
            userInfoVo1.setVideoCount(videos.size());
        }
        map.clear();
        map.put("userInfoVos",userInfoVos);
        map.put("isConcern",isConcern);
        return JsonData.success(map, Const.OPREATE_MSG_SUCCESS);
    }

    /**
     * @需求描述:名师推荐接口业务逻辑层
     * @创建人:whh
     * @创建时间: 2019/11/12
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @Override
    public Map<String, Object> showTeachers(Map<String, Object> map) {
        Integer pageNum = null;
        Integer pageSize = null;
        if (map.get("pageNum") != null){
            pageNum = Integer.parseInt(map.get("pageNum").toString());
        }
        if (map.get("pageSize") != null){
            pageSize = Integer.parseInt(map.get("pageSize").toString());
        }
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize, false);
        }

        UserInfoVo userInfoVo = (UserInfoVo) map.get("userInfoVo");
        map.clear();

        List<UserInfoVo> userInfoVos = userInfoMapper.showTeachers(userInfoVo);
        userInfoVo.setIsTeacher(2);
        Integer teacherCount = userInfoMapper.selectCount(userInfoVo);
        map.put("userInfos",userInfoVos);
        map.put("teacherCount",teacherCount);
        return JsonData.success(map, Const.OPREATE_MSG_SUCCESS);
    }

    /**
     * @需求描述:我的关注(用户关注讲师)接口业务逻辑层
     * @创建人:whh
     * @创建时间: 2019/11/12
     * @修改需求:
     * @修改人:
     * @修改时间:
     * @需求思路:
     */
    @Override
    public Map<String, Object> myCareTeacher(Map<String, Object> map) {
        UserInfoVo userInfoVo = (UserInfoVo) map.get("userInfoVo");
        if (null == userInfoVo.getUserId() || "" == userInfoVo.getUserId()) {
            return JsonData.fail(Const.PARA_IS_NULL);
        }
        //查询讲师
        List<UserInfoVo> userInfoVos = userInfoMapper.myCareTeacher(userInfoVo);

        VideosVo videosVo = new VideosVo();
        videosVo.setCheckStatus(1);
        videosVo.setIsPublish(1);
        videosVo.setIsDel(0);
        for (UserInfoVo userInfoVo1 : userInfoVos) {
            //查询课程结果集
            videosVo.setIsPublish(1);
            videosVo.setUserId(userInfoVo1.getUserId());
            List<Videos> videos = videosMapper.select(videosVo);
            userInfoVo1.setVideosVos(videos);
            userInfoVo1.setVideoCount(videos.size());
        }
        return JsonData.success(userInfoVos, Const.OPREATE_MSG_SUCCESS);
    }

    @Override
    public Map<String, Object> pass(String uiId) {
        UserInfo info = new UserInfo();
        info.setUiId(uiId);
        info.setIsTeacher(2);
        int i = userInfoMapper.updateByPrimaryKeySelective(info);
        return JsonData.returnMap(i);
    }


    @Override
    public Map<String, Object> noPass(String uiId, String noPassReason) {
        UserInfo info = new UserInfo();
        info.setUiId(uiId);
        info.setNoPassReason(noPassReason);
        info.setIsTeacher(3);
        int i = userInfoMapper.updateByPrimaryKeySelective(info);
        return JsonData.returnMap(i);
    }

    @Override
    public Map<String, Object> findCheckTeacherData(Integer page, Integer limit, String userNickName) {
        int size = userInfoMapper.findCheckTeacherData(userNickName).size();
        if (page != null && limit != null) {
            PageHelper.startPage(page, limit, false);
        }
        List<UserInfoVo> list = userInfoMapper.findCheckTeacherData(userNickName);
        return JsonData.returnLayuiMap(list, size);
    }

    @Override
    public Map applyTeacher(String userId, String applyReason, String applyUrl) {
        //验证是否拉黑
        if (!StringUtils.equals("-1", userId) && StringUtils.isNotBlank(userId)) {
            User user = userMapper.selectByPrimaryKey(userId);
            if (user.getUserStatus() == 2) {
                return JsonData.returnMap(811, "您的账号已被锁定");
            }
        }
        Integer flag = userInfoMapper.applyTeacher(userId, applyReason, applyUrl);
        return JsonData.returnMap(flag);
    }
}
